package org.jeecg.modules.KM.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.KM.VO.DocUserVisitPolicyAddVo;
import org.jeecg.modules.KM.VO.DocUserVisitPolicyEditVo;
import org.jeecg.modules.KM.VO.UserVisitPolicyVo;
import org.jeecg.modules.KM.entity.KmDocVisitPolicy;
import org.jeecg.modules.KM.service.IKmDocVisitPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Api(tags = "文档权限范围")
@RestController
@RequestMapping("/KM/kmDocVisitPolicy")
@Slf4j
public class KmDocVisitPolicyController extends JeecgController<KmDocVisitPolicy, IKmDocVisitPolicyService> {
    @Autowired
    private IKmDocVisitPolicyService kmDocVisitPolicyService;

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-分页列表查询")
    @ApiOperation(value = "km_doc_visitPolicy-分页列表查询", notes = "km_doc_visitPolicy-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name = "docId") String docId, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser != null) {
            UserVisitPolicyVo userVisitPolicyVo = new UserVisitPolicyVo();
            QueryWrapper<UserVisitPolicyVo> queryWrapper = QueryGenerator.initQueryWrapper(userVisitPolicyVo, req.getParameterMap());
            String orderBy = queryWrapper.getExpression().getOrderBy().getSqlSegment();
            Page<UserVisitPolicyVo> page = new Page<>(pageNo, pageSize);
            IPage<UserVisitPolicyVo> pageList = kmDocVisitPolicyService.queryPageList(page, docId, orderBy);
            return Result.OK(pageList);
        } else return Result.error("登录信息异常");
    }

    /**
     * 添加
     *
     * @param docUserVisitPolicyAddVo
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-添加")
    @ApiOperation(value = "km_doc_visitPolicy-添加", notes = "km_doc_visitPolicy-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody DocUserVisitPolicyAddVo docUserVisitPolicyAddVo) {
        return kmDocVisitPolicyService.addVisitPolicyDoc(docUserVisitPolicyAddVo);
    }

    /**
     * 编辑
     *
     * @param docUserVisitPolicyEditVo
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-编辑")
    @ApiOperation(value = "km_doc_visitPolicy-编辑", notes = "km_doc_visitPolicy-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody DocUserVisitPolicyEditVo docUserVisitPolicyEditVo) {
        kmDocVisitPolicyService.updateVisitPolicyDoc(docUserVisitPolicyEditVo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-通过id删除")
    @ApiOperation(value = "km_doc_visitPolicy-通过id删除", notes = "km_doc_visitPolicy-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        return kmDocVisitPolicyService.delVisitPolicyDoc(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-批量删除")
    @ApiOperation(value = "km_doc_visitPolicy-批量删除", notes = "km_doc_visitPolicy-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.kmDocVisitPolicyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "km_doc_visitPolicy-通过id查询")
    @ApiOperation(value = "km_doc_visitPolicy-通过id查询", notes = "km_doc_visitPolicy-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        KmDocVisitPolicy kmDocVisitPolicy = kmDocVisitPolicyService.getById(id);
        if (kmDocVisitPolicy == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(kmDocVisitPolicy);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param kmDocVisitPolicy
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KmDocVisitPolicy kmDocVisitPolicy) {
        return super.exportXls(request, kmDocVisitPolicy, KmDocVisitPolicy.class, "km_doc_visitPolicy");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KmDocVisitPolicy.class);
    }

}
